var primitivesTable = new Array();
var isOnSight : boolean = false;

private var minDistance : float = -0.8;
private var agentSpeed : float = 1;
private var moveDirection : Vector3 = Vector3.zero;
private var startPoint : Vector3 = Vector3.zero;

private var rotationMatrix : Quaternion;
private var primitivesPositionTableTemp = new Array();

private var sizeWithEpsilon;
private var lastShotTime = 0;

function Start()
{
	/* setting random move direction */
	moveDirection = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f));

	TransformPrimitivesIntoLocalSpace();

	sizeWithEpsilon = 1.2 * transform.localScale.z;
}

function Update ()
{
	/* check if collision can occur */
	for (cnt = 0; cnt < primitivesPositionTableTemp.length; cnt = cnt + 1)
	{
		var upperEdge : float = primitivesPositionTableTemp[cnt].pos.z + primitivesPositionTableTemp[cnt].scale.z / 2;
		var lowerEdge : float = primitivesPositionTableTemp[cnt].pos.z - primitivesPositionTableTemp[cnt].scale.z / 2;

		/* both edges (upper and lower) are above the upper boundary */
		if ((upperEdge > sizeWithEpsilon) && (lowerEdge > sizeWithEpsilon))
		{
			continue;
		}

		/* both edges (upper and lower) are below the lower boundary */
		if ((upperEdge < -sizeWithEpsilon) && (lowerEdge < -sizeWithEpsilon))
		{
			continue;
		}

		var distanceBeforeAgent : float = (primitivesPositionTableTemp[cnt].pos.x - primitivesPositionTableTemp[cnt].scale.x / 2) -
											(transform.position - startPoint).magnitude;
		/* if distanceBeforeAgent < 0 the object is behind agent, it's a bit tricky because we have to compare with distance being
			travelled from last change of direction */
		if ((distanceBeforeAgent > minDistance) && (distanceBeforeAgent < agentSpeed / 3))
		{
			distanceBeforeAgent = Mathf.Abs(distanceBeforeAgent);
			/* if we get here it means we are on colliding direction*/
			/* the primitive is 'above' agent, we should turn right */
			if (primitivesPositionTableTemp[cnt].scale.z >= 0)
			{
				rotationMatrix = Quaternion.AngleAxis(2 / distanceBeforeAgent, Vector3.up);
			}
			else /* we should turn left */
			{
				rotationMatrix = Quaternion.AngleAxis(-2 / distanceBeforeAgent, Vector3.up);
			}
			moveDirection = rotationMatrix * moveDirection;
			TransformPrimitivesIntoLocalSpace();
			break;
		}
	}

	if (isOnSight)
	{   
		if (Time.time - lastShotTime >= 2)
		{ 
			var player : Player = GameObject.Find("Player").GetComponent("Player") as Player;
			transform.LookAt(player.transform);
			var projectile : GameObject = Instantiate(Resources.Load("ProjectilePrefab"), transform.position, transform.rotation);
			projectile.GetComponent("Projectile").source = "Agent";
			projectile.GetComponent("Projectile").ProjectileSpeed = 2;
			lastShotTime = Time.time;
		}
	}
	/* moving forward */
	transform.position = transform.position + moveDirection.normalized * agentSpeed * Time.deltaTime;
}

/*function invoked on colliding the wall */
function OnTriggerEnter(collider : Collider)
{
	/* the collision occured with horizontal wall*/
	if (collider.transform.position.x == 0)
	{
		moveDirection.z = -moveDirection.z;
	}
	/*collision with vetical wall */
	else if (collider.transform.position.z == 0)
	{
		moveDirection.x = -moveDirection.x;
	}
	TransformPrimitivesIntoLocalSpace();
}

function TransformPrimitivesIntoLocalSpace()
{
	primitivesPositionTableTemp.Clear();

	var angleInDegrees : float = Vector3.Angle(moveDirection, Vector3.right);

	/* third quarter or fourh quarter of the coordinate system */
	if (IsMoveDirectionIn3Or4Quarter())
	{
		angleInDegrees = 360 - angleInDegrees;
	}
	/* we create the rotation specifying what angle and around which axis we want to rotate
		the rotation axis is 'y' */
	rotationMatrix = Quaternion.AngleAxis(angleInDegrees, Vector3.up);

	/* rotate all objects by given angle, the agent is artificially positioned in (0, 0, 0) and moves along the 'x' axis */
	for (var cnt : int = 0; cnt < primitivesTable.length; cnt = cnt + 1)
	{
		primitivesPositionTableTemp.Push(new PrimitiveTransformed( rotationMatrix * (primitivesTable[cnt].transform.position - transform.position),
											  primitivesTable[cnt].transform.localScale));
	}
	startPoint = transform.position;
}

function IsMoveDirectionIn3Or4Quarter()
{
	return ((moveDirection.x < 0 && moveDirection.z < 0) || (moveDirection.x > 0 && moveDirection.z < 0));
}
